package cn.dt70.service.impl;

import cn.dt70.dao.aqbBorrow.AqbBorrowMapper;
import cn.dt70.pojo.AqbBorrow;
import cn.dt70.service.AqbBorrowService;
import cn.dt70.utils.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AqbBorrowServiceImpl implements AqbBorrowService {

    private static final Logger LOGGER = Logger.getLogger(AqbBorrowServiceImpl.class);

    @Resource
    private AqbBorrowMapper aqbBorrowMapper;

    @Override
    public List<AqbBorrow> getAllByUidAndProId(PageSupport ps, String proId, String Uid) {
        List<AqbBorrow> aqbBorrowList = new ArrayList<>();
        try {
            ps.setTotalCount(aqbBorrowMapper.qryCount(proId, Uid));
            aqbBorrowList = aqbBorrowMapper.qryAllByPidAndUid((ps.getCurrentPageNo() - 1) * ps.getPageSize(), ps.getPageSize(), proId, Uid);
        } catch (Exception e) {
            LOGGER.error("AqbBorrowServiceImpl.getAllByUidAndProId 查询异常" + e.getMessage());
        }
        return aqbBorrowList;
    }

    @Override
    public int getCount(String proId, String Uid) {
        int row = 0;
        try {
            row = aqbBorrowMapper.qryCount(proId, Uid);
        } catch (Exception e) {
            LOGGER.error("AqbBorrowServiceImpl.getCount 查询异常" + e.getMessage());
        }
        return row;
    }

    @Override
    public AqbBorrow findBorrowOne(String uid, String pid) {
        AqbBorrow aqbBorrow = null;
        try {
            aqbBorrow = aqbBorrowMapper.selectBorrowOne(uid, pid);
        } catch (Exception e) {
            LOGGER.error("AqbBorrowServiceImpl.findBorrowOne 查询异常" + e.getMessage());
        }
        return aqbBorrow;
    }

    @Override
    public boolean addBorrowOne(AqbBorrow aqbBorrow) {
        boolean flag = false;
        int row = 0;
        try {
            row = aqbBorrowMapper.saveBorrowOne(aqbBorrow);
            if (row != 1) {
                LOGGER.debug("AqbBorrowServiceImpl.addBorrowOne 添加失败");
            } else {
                flag = true;
                LOGGER.debug("AqbBorrowServiceImpl.addBorrowOne 添加成功");
            }
        } catch (Exception e) {
            LOGGER.error("AqbBorrowServiceImpl.addBorrowOne 添加异常" + e.getMessage());
        }

        return flag;
    }

    @Override
    public boolean modifyBorrowInterest(double interest, String bid) {
        boolean flag = false;
        int row = 0;
        try {
            row = aqbBorrowMapper.updateBorrowInterest(interest, bid);
            if (row != 1) {
                LOGGER.debug("AqbBorrowServiceImpl.modifyBorrowInterest 更新失败");
            } else {
                flag = true;
                LOGGER.debug("AqbBorrowServiceImpl.modifyBorrowInterest 更新成功");
            }
        } catch (Exception e) {
            LOGGER.error("AqbBorrowServiceImpl.modifyBorrowInterest 修改利息异常" + e.getMessage());
        }

        return flag;
    }

    @Override
    public boolean modifyBorrow(double selfMoney, double obligation, String bid) {
        boolean flag = false;
        int row = 0;
        try {
            row = aqbBorrowMapper.updateBorrow(selfMoney, obligation, bid);
            if (row != 1) {
                LOGGER.debug("AqbBorrowServiceImpl.modifyBorrow 更新还款金和待还本金失败");
            } else {
                flag = true;
                LOGGER.debug("AqbBorrowServiceImpl.modifyBorrow 更新还款金和待还本金成功");
            }
        } catch (Exception e) {
            LOGGER.error("AqbBorrowServiceImpl.modifyBorrow 修改还款金和待还本金异常" + e.getMessage());
        }

        return flag;
    }
}
